<?php
namespace App\Repository\User;

use App\Facade\Event;
use App\Event\User\MoneyWalletEvent;
use App\Event\User\MoneyWalletRedEvent;

Trait MoneyWallet
{
    // 使用
    public function moneyWalletMake($money, $content)
    {
        if($this->moneyWalletHas($money))
        {
            return $this->moneyWalletAdd(-1 * $money, $content);
        }
    }
    
    // 判断
    public function moneyWalletHas($money)
    {
        if($money > 0)
        {
            return $this->model->money_wallet >= $money;
        }
    }
    
    // 增加
    public function moneyWalletAdd($money, $content)
    {
        $res = $this->model->increment('money_wallet', $money);
        
        Event::dispatch(new MoneyWalletEvent($this, $money, $content));
            
        return $res;
    }
    
    // 分红
    public function moneyWalletRedMake($money, $content)
    {
        if($this->moneyWalletRedHas($money))
        {
            return $this->moneyWalletRedAdd(-1 * $money, $content);
        }
    }
    
    // 判断
    public function moneyWalletRedHas($money)
    {
        if($money > 0)
        {
            return $this->model->money_wallet_red >= $money;
        }
    }
    
    // 增加
    public function moneyWalletRedAdd($money, $content, $type = '')
    {
        $res = $this->model->increment('money_wallet_red', $money);
        
        Event::dispatch(new MoneyWalletRedEvent($this, $money, $content, $type));
            
        return $res;
    }
}